<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.pizzaonline.utils.Utils" %>
<%@ page import="com.pizzaonline.JDBC.*" %>
<%@ page import="java.sql.*" %>

<%
	String status = (String)session.getAttribute("status");

	if (status == null || !status.equals("admin")) {
		Utils.rediriger(out, "index.jsp?err="+Utils.getErrorID("NoAccess"));
	} else {
		DataBase d = new DataBase();
		d.connexionBD();
		Connection c = d.getConnection();
		if (c == null) {
			Utils.rediriger(out, "admin.jsp?err="+Utils.getErrorID("SQLConnProb"));
		} else {
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Pizza Online :: Administration</title>
<link rel="stylesheet" type="text/css" href="css/admin.css" />
<script type="text/javascript" src="js/admin.js"></script>
</head>
<body>

<h1 id="titre">Administration de Pizza Online</h1>
<hr id="titre_sep" />

<h3 class="descr">Colonnes à afficher :</h3>
<form name="affichage" action="admin.jsp" method="post">
<input type="hidden" name="form" value="affichage" />
<ul class="form">
  <li class="left">
    <ul>
      <li class="champ">
        <input type="checkbox" name="colonne" value="id_util" checked="checked" onchange="enableDisable(this)" />id :
      </li>
      <li class="valeur">
<!--
        <span>de : </span>
        <input type="text" name="id_min" size="5" maxlength="20" />
        <span>à : </span>
        <input type="text" name="id_max" size="5" maxlength="20" />
 -->
      </li>

      <li class="champ">
        <input type="checkbox" name="colonne" value="login" checked="checked" onchange="enableDisable(this)" />login :
      </li>
      <li class="valeur">
        <input type="text" name="login" size="10" maxlength="10" />
      </li>

      <li class="champ">
      	<input type="checkbox" name="colonne" value="mdp" onchange="enableDisable(this)" />password
      </li>
      <li class="valeur"></li>

      <li class="champ">
        <input type="checkbox" name="colonne" value="status" checked="checked" onchange="enableDisable(this)" />status :
      </li>
      <li class="valeur">
        <select name="status" size="1" multiple="multiple">
          <option selected="selected">  client  </option>
          <option selected="selected">  chef  </option>
          <option selected="selected">  admin  </option>
        </select>
      </li>
    </ul>
  </li>
  <li class="right">
    <ul>
      <li class="champ">
        <input type="checkbox" name="colonne" value="nom" checked="checked" onchange="enableDisable(this)" />nom :
      </li>
      <li class="valeur">
        <input type="text" name="nom" size="10" maxlength="20" />
      </li>

      <li class="champ">
        <input type="checkbox" name="colonne" value="prenom" checked="checked" onchange="enableDisable(this)" />prénom :
      </li>
      <li class="valeur">
        <input type="text" name="prenom" size="10" maxlength="20" />
      </li>

      <li class="champ">
        <input type="checkbox" name="colonne" value="tel" onchange="enableDisable(this)" />téléphone :
      </li>
      <li class="valeur">
        <input type="text" name="tel" size="10" maxlength="10" disabled="disabled" />
      </li>

      <li class="champ">
        <input type="checkbox" name="colonne" value="adresse" onchange="enableDisable(this)" />adresse :
      </li>
      <li class="valeur">
        <input type="text" name="adresse" size="10" maxlength="100" disabled="disabled" />
      </li>
    </ul>
  </li>
  <li class="submit">
    <input type="submit" value="Afficher" />
  </li>
</ul>
</form>
<%
			String form = request.getParameter("form");
			if (form != null) {
				String[] colonne = request.getParameterValues("colonne");
				if (form.equals("affichage") && colonne != null) {
%>

<div class="info_client">
<h3 class="descr">Utilisateurs sélectionnés :</h3>
<form name="util" action="admin.jsp" method="post">
<input type="hidden" name="form" value="util" />
<input type="submit" value="Supprimer" class="submit" />
<table>
  <thead>
    <tr>
<%
					String reqCols = "id_util";
					String reqVals = "";
					for (int i=0; i<colonne.length; i++) {
						if (!colonne[i].equals("id_util")) {
							reqCols+=","+colonne[i];
						}
						if (colonne[i].equals("status")) {
							String[] vals = request.getParameterValues(colonne[i]); 
							if (vals != null) {
								reqVals += " and ("+colonne[i]+"='"+vals[0]+"'";
								for (int j = 1; j<vals.length; j++) {
									reqVals += " or "+colonne[i]+"='"+vals[j]+"'";
								}
								reqVals += ")";
							}
						} else {
							String val = request.getParameter(colonne[i]);
							if (val != null && !val.trim().equals("")) {
								reqVals += " and UPPER("+colonne[i]+") like '%"+val.toUpperCase()+"%'";
							}
						}
%>
      <th><%= colonne[i] %></th>
<%
					}
%>
      <th>supprimer</th>
    </tr>
  </thead>
  <tbody>
<%
					ResultSet res = null;
					if (reqCols.startsWith(",")) {
						reqCols = reqCols.substring(1, reqCols.length());
					}
					if (reqVals.startsWith(" and ")) {
						reqVals = reqVals.substring(5, reqVals.length());
					}
					try {
						res = d.select(reqCols, "utilisateur", reqVals);
						while (res.next()) {
%>
    <tr onmouseover="selectDisplay(this);" onmouseout="deselectDisplay(this);">
<%
							for (int i=0; i<colonne.length; i++) {
%>
      <td onclick="afficheFormulaire(this);"><%= res.getString(colonne[i]) %></td>
<%
							}
%>
      <td class="del"><input type="checkbox" name="suppr" value="<%= res.getString("id_util") %>" onchange="deleteDisplay(this);" /></td>
    </tr>
<%
						}
					} catch (SQLException e) {
						out.print("DEBUG SQL ERROR : <br />" + e + "<br />");
					}
%>
  </tbody>
</table>
<input type="submit" value="Supprimer" class="submit" />
</form>
</div>
</body>
</html>
<%	
				} else if (form.equals("util")) {
					String[] ids = request.getParameterValues("suppr");
					if (ids != null && ids.length >= 1) {
						String reqVals = "id_util='"+ids[0]+"'";

						for (int i=1; i<ids.length; i++) {
							reqVals += " or id_util='"+ids[i]+"'";
						}
						try {
							d.delete("utilisateur", reqVals);
						} catch (SQLException e) {
							out.print("DEBUG SQL ERROR : <br />" + e + "<br />");
						}
					}
					Utils.rediriger(out, "admin.jsp");
				} else if (form.equals("modif")) {
					String[] champs = new String[] {"id_util", "login", "mdp", "nom", "prenom", "tel", "adresse", "status"};
					//String[] champs = new String[] {"login", "mdp", "nom", "prenom", "tel", "adresse", "status"};
					String reqVals = "";
					for (int i=0; i<champs.length; i++) {
						String val = request.getParameter(champs[i]);
						if (!val.equals(request.getParameter(champs[i]+"_def"))) {
							if (champs[i].equals("mdp")) {
								reqVals += ","+champs[i]+"=md5('"+val+"')";
							} else {
								reqVals += ","+champs[i]+"='"+val+"'";
							}
						}
					}
					if (reqVals.startsWith(",")) {
						reqVals = reqVals.substring(1, reqVals.length());
					}
					try {
						d.update("utilisateur", reqVals,"id_util='"+request.getParameter("id_util_def")+"'");
						out.print("<html><head><title>Requete effectuee</title></head><body><h2>Requête effectuée avec succès</h2></body></html>");
					} catch (SQLException e) {
						out.print("DEBUG SQL ERROR : <br />" + e + "<br />");
					}
					//Utils.rediriger(out, "admin.jsp");
				}
			}
		d.closeConnection(); // fermer la connexion
		}
	}
%>
